<template>
  <div class="app-container">
    <div style="width:100%; text-align: right;">
      <el-button size="small" type="primary" icon="el-icon-download" @click="exportData">导出</el-button>
    </div>
    <div style="width: 100%; margin-top: 8px;">
      <el-form :model="searchForm" label-width="80px">
        <div style="display: flex;">
          <el-form-item label="关键字">
            <el-input v-model="searchForm.searchValue" size="small" placeholder="访客姓名 | 单位 | 手机号" style="width: 160px;"></el-input>
          </el-form-item>
          <el-form-item label="接待部门">
            <el-select v-model="searchForm.receiverDeptId" placeholder="请选择">
              <el-option
                  v-for="item in deptOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="审批状态" style="margin-left: 10px;width: 170px;">
            <el-select v-model="searchForm.examineStatus" placeholder="请选择">
              <el-option
                  v-for="item in statusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间" style="margin-left: 10px;">
            <el-date-picker
                v-model="searchForm.searchBeginDate"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择开始时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" style="margin-left: 10px;">
            <el-date-picker
                style="display: inline-block;"
                v-model="searchForm.searchEndDate"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择结束时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-button-group style="margin-left: 16px;">
            <el-button type="primary" plain icon="el-icon-search" @click="search">搜索</el-button>
            <el-button @click="refresh">重置</el-button>
          </el-button-group>
        </div>
      </el-form>
    </div>

    <el-divider />

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" stripe>
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="登记时间" prop="createTime" />
      <el-table-column label="来访单位" prop="visitUnit" />
      <el-table-column label="访客类型">
        <template slot-scope="scope">
          {{ scope.row.type=='0'? '普通' : '团体' }}
        </template>
      </el-table-column>
      <el-table-column label="来访人" prop="visitPersonName" />
      <el-table-column label="手机号" prop="phone" />
      <el-table-column label="身份证号" prop="cardNo" />
      <el-table-column label="有效期始" prop="visitBeginDate" />
      <el-table-column label="有效期止" prop="visitEndDate" />
      <el-table-column label="审批状态">
        <template slot-scope="scope">
          {{ scope.row.status=='0'? '待审' : (scope.row.status=='1' ? '通过':'未通过') }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="80" align="center">
        <template slot-scope="scope">
          <span>
            <el-button circle type="success" icon="el-icon-more" size="mini" @click="detail(scope.row.id)" title="详情"></el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize"
      @pagination="fetchData" style="text-align: center" />

    <el-dialog :visible.sync="dialogVisible" title="详情" width="65%" append-to-body>
      <el-form ref="dataForm" :model="detailData" label-width="120px" label-position="right" v-if="detailData">
        <el-row :gutter="20">
          <el-col :span="16">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="ID号">
                  <span>{{ detailData.id }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="审批状态">
                  <span>{{ detailData.status==0? '未审' : (detailData.status==1 ? '通过':'未通过') }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="登记时间">
                  <span>{{ detailData.createTime }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="接待人">
                <span>{{ detailData.receiverName }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="接待人手机">
                <span>{{ detailData.receiverPhone }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="接待部门">
                  <span>{{ detailData.receiverDept }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="有效期始">
                  <span>{{ detailData.visitBeginDate }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="有效期止">
                  <span>{{ detailData.visitEndDate }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="重复进入">
                  <span>{{ detailData.reaptIo === 1 ? '允许' : '禁止' }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="来访单位">
                  <span>{{ detailData.visitUnit }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="到访原因">
                  <span>{{ detailData.visitReason }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="访客类型">
                  <span>
                    <template v-if="detailData.type === 0">普通访客</template>
                    <template v-if="detailData.type === 2">团体访客</template>
                  </span>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <template v-if="detailData.type === 0">
                  <el-form-item label="来访人员">
                    <template v-for="(item,index) in detailData.persons">
                      <el-tag @click="showPersonDetail(index)" style="cursor: pointer;">{{item.name}}</el-tag>
                    </template>
                  </el-form-item>
                </template>
                <template v-else>
                  <el-form-item label="团体代表">
                    <template v-for="(item,index) in detailData.persons">
                      <el-tag @click="showPersonDetail(index)" style="cursor: pointer;">{{item.name}}</el-tag>
                    </template>
                  </el-form-item>
                </template>
              </el-col>
              <el-col :span="24">
                <el-form-item label="登记车辆">
                  <template v-for="(item,index) in detailData.cars">
                      <el-tag @click="showCarDetail(index)" style="cursor: pointer;">{{item.carNo}}</el-tag>
                  </template>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注">
                  <span>{{ detailData.remarks }}</span>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="8">
            <template v-if="detailData.views.length > 0">
              <el-col :span="24">
                <el-card class="box-card" shadow="never">
                  <div slot="header" class="clearfix">
                    <span>审批信息</span>
                  </div>
                  <el-timeline>
                    <el-timeline-item
                        v-for="(item,index) in detailData.views"
                        :key="index"
                        :timestamp="formatTime(item.date)">
                      <span>审批人：{{item.name}}</span>
                      <span><br/>审批结果：
                        <template v-if="item.examineStatus === 1">
                          <el-tag type="success">通过</el-tag>
                        </template>
                        <template v-else>
                          <el-tag type="danger">不通过</el-tag>
                        </template>
                      </span>
                      <template v-if="item.view">
                        <br/>
                        <span>审批意见：{{item.view}}</span>
                      </template>
                    </el-timeline-item>
                  </el-timeline>
                </el-card>
              </el-col>
            </template>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="personDlgVisible" title="人员详情" width="50%" append-to-body>
      <el-form :model="personDetail" label-width="120px" label-position="right" v-if="personDetail">
        <el-row :gutter="20">
          <el-col :span="16">
            <el-row>
              <el-col :span="12">
                <!--
                <el-upload
                    :data="uploadParam3s"
                    class="avatar-uploader"
                    :action="APIURL"
                    :show-file-list="false"
                    accept="image/*"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                  <img v-if="personDetail.pictureFile" :src="personDetail.pictureFile" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                -->
                <img v-if="personDetail.pictureFile" :src="personDetail.pictureFile" class="avatar">
              </el-col>
              <el-col :span="12">
                <el-form-item label="ID号">
                  <span>{{ personDetail.id }}</span>
                </el-form-item>
                <el-form-item label="姓名">
                  <span>{{ personDetail.name }}</span>
                </el-form-item>
                <el-form-item label="访客类型">
                  <template v-if="detailData.type === 0">
                    <span>{{personDetail.type === 0 ? '来访人员' : '同行人员'}}</span>
                  </template>
                  <template v-else-if="detailData.type === 2">
                    <span>团体代表</span>
                  </template>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <template v-if="detailData.type === 2">
                <el-col :span="12">
                  <el-form-item label="随行人数">
                    <span>{{ personDetail.personNumber }}</span>
                  </el-form-item>
                </el-col>
              </template>
              <el-col :span="12">
                <el-form-item label="身份证号">
                  <span>{{ personDetail.cardNo }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="手机号">
                  <span>{{ personDetail.phone }}</span>
                </el-form-item>
              </el-col>
              <template v-if="personDetail.status > 0">
                <el-col :span="24">
                  <el-form-item label="访客卡号">
                    <span>{{ personDetail.cardCode }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="领卡时间">
                    <span>{{ personDetail.getCardTime }}</span>
                  </el-form-item>
                </el-col>
              </template>
            </el-row>
          </el-col>
          <el-col :span="8">
            <template v-if="personDetail.actions.length > 0">
              <el-col :span="24">
                <el-card class="box-card" shadow="never">
                  <div slot="header" class="clearfix">
                    <span>人员行踪</span>
                  </div>
                  <el-timeline>
                    <el-timeline-item
                        v-for="(item,index) in personDetail.actions"
                        :key="index"
                        :timestamp="formatTime(item.date)">
                      <span>地点：{{item.place}}</span>
                      <span><br/>动作：
                        <template v-if="item.action === 1">
                          <el-tag>领卡</el-tag>
                        </template>
                        <template v-else-if="item.action === 2">
                          <el-tag type="success">入场</el-tag>
                        </template>
                        <template v-else-if="item.action === 3">
                          <el-tag type="info">出场</el-tag>
                        </template>
                        <template v-else>
                          <el-tag type="warning">退卡</el-tag>
                        </template>
                      </span>
                    </el-timeline-item>
                  </el-timeline>
                </el-card>
              </el-col>
            </template>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="carDlgVisible" title="车辆详情" width="50%" append-to-body>
      <el-card class="box-card" shadow="never" v-if="carDetail">
        <div slot="header" class="clearfix">
          <span>{{ carDetail.carNo }}&nbsp;出入信息</span>
        </div>
        <template v-if="carDetail.inOuts.length ===0">
          <div>未入场</div>
        </template>
        <template v-else>
          <el-timeline>
            <el-timeline-item
                v-for="(item,index) in carDetail.inOuts"
                :key="index"
                :timestamp="formatTime(item.date)">
              <span>{{item.date}}</span>
              <span>由{{item.gateName}}</span>
              <span>
                <template v-if="item.direction === 1">
                  <el-tag>出场</el-tag>
                </template>
                <template v-else>
                  <el-tag type="warning">入场</el-tag>
                </template>
              </span>
            </el-timeline-item>
          </el-timeline>
        </template>
      </el-card>
    </el-dialog>

  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import {
    visitList,
    visitInfo,
    visitExport
  } from '@/api/visit'
  import {datetimeFormat} from '@/utils/datetime-util'
  import {
    deptList
  } from '@/api/process'
  import  {buildUploadParams} from '@/api/test'

  export default {
    name: 'IndexVisitor',
    components: {
      Pagination
    },
    data() {
      return {
        imageUrl: '',
        // APIURL: process.env.VUE_APP_BASE_API + '/system/visitorCheck/uploadVisitorPicture',
        // uploadParams: {},
        personDlgVisible: false,
        personDetail: null,
        carDlgVisible: false,
        carDetail: null,
        statusOptions: [
          {value: '', label: '全部'},
          {value: '0', label: '待审'},
          {value: '1', label: '已通过'},
          {value: '2', label: '已拒绝'},
        ],
        deptOptions: [],
        total: 0,
        list: [],
        listLoading: false,
        searchForm: {
          pageNum: 1,
          pageSize: 10,
          searchValue: '',
          searchBeginDate: '',
          searchEndDate: '',
          examineStatus: '',
          receiverDeptId: ''
        },
        dialogVisible: false,
        dialogType: 'create',
        loading: false,
        detailData: null,
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      }
    },
    created() {
      this.getDeptList()
    },
    mounted() {
      this.fetchData()
    },
    methods: {
      exportData() {
        let queryParams = {
          searchValue: this.searchForm.searchValue,
          searchBeginDate: this.searchForm.searchBeginDate,
          searchEndDate: this.searchForm.searchEndDate,
          examineStatus: this.searchForm.examineStatus,
          receiverDeptId: this.searchForm.receiverDeptId
        }
        visitExport(queryParams).then(response => {
          if(response.code === 200) {
            let filename = response.msg
            let url = '/download/' + filename
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            link.setAttribute("download", filename); //指定下载后的文件名，防跳转
            document.body.appendChild(link);
            link.click();
          }
        })
      },
      /*
      handleAvatarSuccess(res, file) {
        if (res.code === 200) {
          this.detail(this.personDetail.id)
        }
      },
      beforeAvatarUpload(file) {
        console.log(file)
        const isImage = file.type.startsWith('image/');
        const isLt5M = file.size / 1024 / 1024 < 5;

        if (!isImage) {
          this.$message.error('上传头像图片只能是 图片 格式!');
        }
        if (!isLt5M) {
          this.$message.error('上传头像图片大小不能超过 5MB!');
        }
        return isImage && isLt5M
      },*/
      showCarDetail(index) {
        this.carDetail = this.detailData.cars[index]
        this.carDlgVisible = true
      },
      showPersonDetail(index) {
        this.personDetail = this.detailData.persons[index]
        this.personDlgVisible = true
        /*
        this.uploadParams = buildUploadParams('742ac0908829a0a4d5', this.personDetail.id);
        console.log('this.uploadParams', this.uploadParams)*/
      },
      getDeptName(deptId) {
        if (deptId === '1') {
          return ''
        }
        let name = '未知'
        for (let i = 0; i < this.deptOptions.length; i++) {
          if (this.deptOptions[i].value === deptId) {
            let parentName = ''
            if (this.deptOptions[i].parentId !== 1) {
              // 找上级部门
              for (let j = 0; j < this.deptOptions.length; j++ ) {
                if (this.deptOptions[j].value === this.deptOptions[i].parentId) {
                  parentName = this.deptOptions[j].label
                }
              }
            }
            if (parentName) {
              name = parentName + '-'  + this.deptOptions[i].label
            } else {
              name = this.deptOptions[i].label
            }
            break
          }
        }
        return name
      },
      getDeptList() {
        deptList().then(response => {
          console.log('getDeptList', response)
          if (response.code === 200) {
            this.deptOptions = []
            for (let i = 0; i < response.rows.length; i++) {
              let obj = response.rows[i]
              this.deptOptions.push({
                value: '' + obj.deptId,
                label: obj.name,
                parentId: '' + obj.parentId
              })
            }
            console.log('this.deptOptions', this.deptOptions)
          } else {
            this.$message.error('获取部门列表失败' + response.errmsg)
          }
        })
      },
      formatTime(timeStr) {
        return datetimeFormat('YYYY-mm-dd HH:MM:SS', new Date(timeStr))
      },
      search() {
        this.fetchData()
      },
      refresh() {
        this.searchForm = {
          pageNum: 1,
          pageSize: 10,
          searchValue: '',
          searchBeginDate: '',
          searchEndDate: '',
          examineStatus: '',
          receiverDeptId: ''
        }
        this.fetchData()
      },
      fetchData() {
        this.listLoading = true
        visitList(this.searchForm).then(response => {
          this.list = response.rows
          this.total = response.total
          this.listLoading = false
        })
      },
      detail(id) {
        visitInfo(id).then(response => {
          if (response.code === 200) {
            this.detailData = response.data
            this.dialogVisible = true
          } else {
            this.$message.error('获取详情失败：' + response.msg)
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-upload {
    border: 1px dashed #d9d9d9 !important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    .el-icon-plus.avatar-uploader-icon {
      border: 1px dashed #d9d9d9 !important;
      border-radius: 6px;
      font-size: 28px;
      color: #8c939d;
      width: 128px;
      height: 128px;
      line-height: 128px;
      text-align: center;
    }
  }

  .avatar-uploader {
    height: 128px;

    img {
      width: 128px;
      height: 128px;
    }
  }

  .avatar {
    height: 160px;
    margin-left: 16px;
  }
</style>
